<!DOCTYPE html>
<html lang="zh_CN">
<head>

<link rel="stylesheet" type="text/css"
	href="../css/dist/bootstrap-combined.css">
<link rel="stylesheet" type="text/css" href="../css/dist/layoutit.css">
<link rel="stylesheet" type="text/css" href="../css/jqui/jquery-ui.css">
<script src="../css/dist/jquery-1.11.2.min.js"></script>
<script src="../css/dist/bootstrap.min.js"></script>

<script src="../css/jqui/jquery-ui.js"></script>
<script src="../css/dist/alert.js"></script>
<script type="text/javascript" src="../ui/jslib/rainbow/json2.min.js"></script>
<script type="text/javascript" src="../ui/jslib/rainbow/rainbow.js"></script>
<script type="text/javascript" src="../ui/jslib/rainbow/rainbowAjax.js"></script>
<script type="text/javascript"
	src="../ui/jslib/rainbow/rainbowDialog.js"></script>
<script type="text/javascript" src="../ui/jslib/rainbow/rainbowutil.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
tr input {
	width: 70px;
}

form input {
	width: 300px;
}
</style>
<script type="text/javascript">
	var canAdd = true;

	$(document).ready(function() {
		initTable();
		
		$("form input").click(function(){
			$("#add_err_msg_div").hide();
		})
	});
	
	function openAddErr(msg){
		$("#add_err_msg").html(msg);
		$("#add_err_msg_div").show("slow");
	}

	function initTable() {
		var rainbow = new Rainbow();
		rainbow.setService("snmpservice");
		rainbow.setMethod("query");
		sendCommand("../dispatcherAction/execute.do", rainbow, "post",
				new queryCallback());
	}

	var queryCallback = function(tr) {
		this.onSuccess = function(data) {
			$.each(data.rows, function(index, obj) {
				var tr = "<tr><td>";
				tr += obj.name + "</td><td>";
				tr += obj.ip + "</td><td>";
				tr += obj.port + "</td><td>";
				tr += obj.community + "</td><td>";
				tr += "</td><td><a class='btn' onclick=\"del(this,'"
						+ obj.guid + "')\">删除</a></td></tr>";
				$("#table1 tbody tr:last").before(tr)
			});
		};
		this.onFail = function(jqXHR, textStatus, errorThrown) {
			alert("error");
		};
	}

	function del(o, guid) {
		if (confirm("确定要删除该连接吗?")) {
			var rainbow = new Rainbow();
			rainbow.attr = {
				guid : guid
			}
			rainbow.setService("snmpservice");
			rainbow.setMethod("delete");
			sendCommand("../dispatcherAction/execute.do", rainbow, "post",
					new returnDel(o));
		}
	}

	var returnDel = function(o) {
		this.onSuccess = function(data) {
			if (data.success) {
				AlertMsg("删除成功");
				$(o).parent().parent().remove();
			} else {
				alert("删除失败," + data.msg);
			}
		};
		this.onFail = function(jqXHR, textStatus, errorThrown) {
			alert("error");
		};
	}

	function addTr(o) {
		if (!canAdd) {
			alert("请先完成未完成的创建");
			return;
		}
		canAdd = false;
		var tr = $(o).parent().parent();
		tr
				.before("<tr><td><input type=\"text\"></td><td><input type=\"text\"></td><td><input type=\"text\"></td><td><input type=\"text\"></td><td><input type=\"text\"></td><td><input type=\"text\"></td><td></td><td><a class='btn' onclick='create(this)'>创建</a><a class='btn' onclick='cancel(this)'>取消</a></td></tr>")
	}

	function cancel(o) {
		canAdd = true;
		$(o).parent().parent().remove();
	}

	function create() {
		var snmpname = $("#snmpname").val();
		var ip = $("#ip").val();
		var port = $("#port").val();
		var community = $("#community").val();
		if (snmpname == null || snmpname.length == 0) {
			openAddErr("请输入jmx名称");
			return;
		}
		if (ip == null || ip.length == 0) {
			openAddErr("请输入ip");
			return;
		}
		if (port == null || port.length == 0) {
			openAddErr("请输入端口");
			return;
		}
		if (community == null || community.length == 0) {
			openAddErr("请输入community");
			return;
		}

		var data = {
			snmpname:snmpname,
			ip : ip,
			port : port,
			community : community
		};

		var rainbow = new Rainbow();
		rainbow.setAttr(data);
		rainbow.setService("snmpservice");
		rainbow.setMethod("insert");
		sendCommand("../dispatcherAction/execute.do", rainbow, "post",
				new addcallback());
	}

	var addcallback = function() {
		this.onSuccess = function(data) {
			if (data.success) {
				window.location.href = window.location.href;
			} else {
				openAddErr("创建失败," + data.msg);
			}
		};
		this.onFail = function(jqXHR, textStatus, errorThrown) {
			alert("error");
		};
	}
</script>
</head>
<div class="row-fluid">
	<div class="span12">
		<blockquote>
			<p>SNMP接参数配置.</p>
			<p>
				<small>配置一个JMX连接参数,<font color="red">注：只限于linux操作系统监控!</font>   <a href="http://note.youdao.com/share/?id=69a9f8d3d1392b4525efb8f9ebc7b404&type=note">linux snmp安装</a></small>
			</p>
			
		</blockquote>
		<table class="table table-hover table-striped table-condensed"
			id="table1">
			<thead>
				<tr>
					<th>snmp名称</th>
					<th>ip</th>
					<th>端口</th>
					<th>community</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td colspan="6">
						<!-- <a class="btn btn-primary" onclick="addTr(this);">添加新的连接...</a>  -->
						<a id="modal-164474" href="#modal-container-164474" role="button"
						class="btn btn-primary" data-toggle="modal">添加新的连接...</a>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

<div id="modal-container-164474" class="modal hide fade" role="dialog"
	aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal"
			aria-hidden="true">×</button>
		<h3 id="myModalLabel">数据源配置</h3>
	</div>
	<div class="modal-body">
		<form class="form-horizontal">
		<div class="control-group">
				<div class="col-sm-12" align="center" style="margin-top: 5px;">
					<input id="snmpname" type="text" placeholder="请输入snmp名称" />
				</div>
			</div>
			<div class="control-group">
				<div class="col-sm-12" align="center" style="margin-top: 5px;">
					<input id="ip" type="text" placeholder="请输入IP" />
				</div>
			</div>
			<div class="control-group">
				<div class="col-sm-12" align="center" style="margin-top: 5px;">
					<input id="port" type="text" value="161" placeholder="请输入端口,defult:161" />
				</div>
			</div>
			<div class="control-group">
				<div class="col-sm-12" align="center" style="margin-top: 5px;">
					<input id="community" type="text" value="public" placeholder="请输入community,defult:public" />
				</div>
			</div>
			<div class="control-group" id="add_err_msg_div" style="display: none;">
				<div class="col-sm-12" align="center" style="margin-top: 5px;">
					<div class="alert alert-info" role="alert"  id="add_err_msg">用户名或密码不能为空</div>
				</div>
			</div>
		</form>
	</div>
	<div class="modal-footer">
		<button class="btn btn-primary" onclick="create()">保存设置</button>
		<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
	</div>
</div>
</html>